package com.example.redisdemo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.example.entity.User;
import com.example.util.MongoDBHelper;

@Service
public class MongoServiceTest<T> {

	@Resource
	private MongoDBHelper<User> mogoUtil;
	@Resource
	private MongoTemplate mongoTemplate;

	/**
	 * 查询全部
	 *
	 * @return ApiResponse
	 */
	public void findStudents() {
		mongoTemplate.insert(new User("test", 23));
		System.out.println(mongoTemplate.findAll(User.class));
		
		Query query = new Query(Criteria.where("username").is("test"));
		System.out.println(query);
		
		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 10; i++) {
			users.add(new User("test"+i, 23));
		}
		mongoTemplate.insertAll(users);
	}
	
	
	public void findStudentsByCondition() {
//		查找所有用户
		System.out.println(mongoTemplate.findAll(User.class));
//		根据条件查找用户
		Query query = new Query(Criteria.where("username").is("test"));
		List<User> find = mongoTemplate.find(query, User.class);
		System.out.println(find);
	}
	
	public void deleteUser() {
//		Query query=new Query();
//		Criteria criteria=Criteria.where("name").is("tom").and("studentNum").in(numberList);
//		query.addCriteria(criteria); 
		Query query = new Query(Criteria.where("username").is("test"));
		mongoTemplate.remove(query, User.class);
		System.out.println(mongoTemplate.findAll(User.class));
	}

	
}
